home *** CD-ROM | disk | FTP | other *** search
/ Programmer Power Tools / Programmer Power Tools.iso / progjrn / pj_6_2.arc / ONGRAF62.L2 < prev    next >
Text File  |  1988-02-18  |  7KB  |  332 lines

  1. Code from "Yet Another VGA Write Mode" by Michael Abrash, PJ,
  2. Volume 6.2.  Copyright 1987 Michael Abrash. 
  3. ; *** Listing 2 ***
  4. ; Program to illustrate one use of write mode 2 of the VGA and EGA by 
  5. ; drawing lines in color patterns.  
  6. ; Assembled with MASM 5.0, linked with MS-LINK 3.60.  
  7. ; By Michael Abrash 11/8/87 
  8. ; Stack segment para stack 'STACK'
  9.             db    512 dup(0)
  10. Stack    ends
  11.  
  12. SCREEN_WIDTH_IN_BYTES    equ    80
  13. GRAPHICS_SEGMENT equ    0a000h    ;mode 10 bit-map segment
  14. SC_INDEX    equ    3c4h    ;Sequence Controller Index register
  15. MAP_MASK    equ    2    ;index of Map Mask register
  16. GC_INDEX    equ    03ceh    ;Graphics Controller Index reg
  17. GRAPHICS_MODE    equ    5    ;index of Graphics Mode reg
  18. BIT_MASK    equ    8    ;index of Bit Mask reg
  19.  
  20. Data    segment    para common 'DATA'
  21. Pattern0    db    16
  22.         db    0, 1, 2, 3, 4, 5, 6, 7, 8
  23.         db    9, 10, 11, 12, 13, 14, 15
  24. Pattern1    db    6
  25.         db    2, 2, 2, 10, 10, 10
  26. Pattern2    db    8
  27.         db    15, 15, 15, 0, 0, 15, 0, 0
  28. Pattern3    db    9
  29.         db    1, 1, 1, 2, 2, 2, 4, 4, 4
  30. Data    ends
  31.  
  32. Code    segment    para public 'CODE'
  33.     assume    cs:Code, ds:Data
  34. Start    proc    near
  35.     mov    ax,Data
  36.     mov    ds,ax
  37.     mov    ax,10h
  38.     int    10h        ;select video mode 10h (640x350)
  39. ;
  40. ; Draw 8 radial lines in upper left quadrant in pattern 0.
  41. ;
  42.     mov    bx,0
  43.     mov    cx,0
  44.     mov    si,offset Pattern0
  45.     call    QuadrantUp
  46. ;
  47. ; Draw 8 radial lines in upper right quadrant in pattern 1.
  48. ;
  49.     mov    bx,320
  50.     mov    cx,0
  51.     mov    si,offset Pattern1
  52.     call    QuadrantUp
  53. ;
  54. ; Draw 8 radial lines in lower left quadrant in pattern 2.
  55. ;
  56.     mov    bx,0
  57.     mov    cx,175
  58.     mov    si,offset Pattern2
  59.     call    QuadrantUp
  60. ;
  61. ; Draw 8 radial lines in lower right quadrant in pattern 3.
  62. ;
  63.     mov    bx,320
  64.     mov    cx,175
  65.     mov    si,offset Pattern3
  66.     call    QuadrantUp
  67. ;
  68. ; Wait for a key before returning to text mode and ending.
  69. ;
  70.     mov    ah,01h
  71.     int    21h
  72.     mov    ax,03h
  73.     int    10h
  74.     mov    ah,4ch
  75.     int    21h
  76. ;
  77. ; Draws 8 radial lines with specified pattern in specified mode 10h
  78. ; quadrant.
  79. ;
  80. ; Input:
  81. ;    BX = X coordinate of upper left corner of quadrant
  82. ;    CX = Y coordinate of upper left corner of quadrant
  83. ;    SI = pointer to pattern, in following form:
  84. ;        Byte 0: Length of pattern
  85. ;        Byte 1: Start of pattern, one color per byte
  86. ;
  87. ; AX, BX, CX, DX destroyed
  88. ;
  89. QuadrantUp    proc    near
  90.     add    bx,160
  91.     add    cx,87        ;point to the center of the quadrant
  92.     mov    ax,0
  93.     mov    dx,160
  94.     call    LineUp        ;draw horizontal line to right edge
  95.     mov    ax,1
  96.     mov    dx,88
  97.     call    LineUp        ;draw diagonal line to upper right
  98.     mov    ax,2
  99.     mov    dx,88
  100.     call    LineUp        ;draw vertical line to top edge
  101.     mov    ax,3
  102.     mov    dx,88
  103.     call    LineUp        ;draw diagonal line to upper left
  104.     mov    ax,4
  105.     mov    dx,161
  106.     call    LineUp        ;draw horizontal line to left edge
  107.     mov    ax,5
  108.     mov    dx,88
  109.     call    LineUp        ;draw diagonal line to lower left
  110.     mov    ax,6
  111.     mov    dx,88
  112.     call    LineUp        ;draw vertical line to bottom edge
  113.     mov    ax,7
  114.     mov    dx,88
  115.     call    LineUp        ;draw diagonal line to bottom right
  116.     ret
  117. QuadrantUp    endp
  118. ;
  119. ; Draws a horizontal, vertical, or diagonal line (one of the eight
  120. ; possible radial lines) of the specified length from the specified
  121. ; starting point.
  122. ;
  123. ; Input:
  124. ;    AX = line direction, as follows:
  125. ;        3  2  1
  126. ;        4  *  0
  127. ;        5  6  7
  128. ;    BX = X coordinate of starting point
  129. ;    CX = Y coordinate of starting point
  130. ;    DX = length of line (number of pixels drawn)
  131. ;
  132. ; All registers preserved.
  133. ;
  134. ; Table of vectors to routines for each of the 8 possible lines.
  135. ;
  136. LineUpVectors    label    word
  137.     dw    LineUp0, LineUp1, LineUp2, LineUp3
  138.     dw    LineUp4, LineUp5, LineUp6, LineUp7
  139.  
  140. ;
  141. ; Macro to draw horizontal, vertical, or diagonal line.
  142. ;
  143. ; Input:
  144. ;    XParm = 1 to draw right, -1 to draw left, 0 to not move horz.
  145. ;    YParm = 1 to draw up, -1 to draw down, 0 to not move vert.
  146. ;    BX = X start location
  147. ;    CX = Y start location
  148. ;    DX = number of pixels to draw
  149. ;    DS:SI = line pattern
  150. ;
  151. MLineUp    macro    XParm, YParm
  152.     local    LineUpLoop, CheckMoreLine
  153.     mov    di,si        ;set aside start offset of pattern
  154.     lodsb            ;get length of pattern
  155.     mov    ah,al
  156.  
  157. LineUpLoop:
  158.     lodsb            ;get color of this pixel...
  159.     call    DotUpInColor    ;...and draw it
  160. if XParm EQ 1
  161.     inc    bx
  162. endif
  163. if XParm EQ -1
  164.     dec    bx
  165. endif
  166. if YParm EQ 1
  167.     inc    cx
  168. endif
  169. if YParm EQ -1
  170.     dec    cx
  171. endif
  172.     dec    ah        ;at end of pattern?
  173.     jnz    CheckMoreLine
  174.     mov    si,di        ;get back start of pattern
  175.     lodsb
  176.     mov    ah,al        ;reset pattern count
  177.  
  178. CheckMoreLine:
  179.     dec    dx
  180.     jnz    LineUpLoop
  181.     jmp    LineUpEnd
  182.     endm
  183.  
  184. LineUp    proc    near
  185.     push    ax
  186.     push    bx
  187.     push    cx
  188.     push    dx
  189.     push    si
  190.     push    di
  191.     push    es
  192.  
  193.     mov    di,ax
  194.  
  195.     mov    ax,GRAPHICS_SEGMENT
  196.     mov    es,ax
  197.  
  198.     push    dx        ;save line length
  199. ;
  200. ; Enable writes to all planes.
  201. ;
  202.     mov    dx,SC_INDEX
  203.     mov    al,MAP_MASK
  204.     out    dx,al
  205.     inc    dx
  206.     mov    al,0fh
  207.     out    dx,al
  208. ;
  209. ; Select write mode 2.
  210. ;
  211.     mov    dx,GC_INDEX
  212.     mov    al,GRAPHICS_MODE
  213.     out    dx,al
  214.     inc    dx
  215.     mov    al,02h
  216.     out    dx,al
  217. ;
  218. ; Vector to proper routine.
  219. ;
  220.     pop    dx        ;get back line length
  221.  
  222.     shl    di,1
  223.     jmp    cs:[LineUpVectors+di]
  224. ;
  225. ; Horizontal line to right.
  226. ;
  227. LineUp0:
  228.     MLineUp    1, 0
  229. ;
  230. ; Diagonal line to upper right.
  231. ;
  232. LineUp1:
  233.     MLineUp    1, -1
  234. ;
  235. ; Vertical line to top.
  236. ;
  237. LineUp2:
  238.     MLineUp    0, -1
  239. ;
  240. ; Diagonal line to upper left.
  241. ;
  242. LineUp3:
  243.     MLineUp    -1, -1
  244. ;
  245. ; Horizontal line to left.
  246. ;
  247. LineUp4:
  248.     MLineUp    -1, 0
  249. ;
  250. ; Diagonal line to bottom left.
  251. ;
  252. LineUp5:
  253.     MLineUp    -1, 1
  254. ;
  255. ; Vertical line to bottom.
  256. ;
  257. LineUp6:
  258.     MLineUp    0, 1
  259. ;
  260. ; Diagonal line to bottom right.
  261. ;
  262. LineUp7:
  263.     MLineUp    1, 1
  264.  
  265. LineUpEnd:
  266.     pop    es
  267.     pop    di
  268.     pop    si
  269.     pop    dx
  270.     pop    cx
  271.     pop    bx
  272.     pop    ax
  273.     ret
  274. LineUp    endp
  275. ;
  276. ; Draws a dot in the specified color at the specified location.
  277. ; Assumes that the VGA is in write mode 2 with writes to all planes
  278. ; enabled and that ES points to display memory.
  279. ;
  280. ; Input:
  281. ;    AL = dot color
  282. ;    BX = X coordinate of dot
  283. ;    CX = Y coordinate of dot
  284. ;    ES = display memory segment
  285. ;
  286. ; All registers preserved.
  287. ;
  288. DotUpInColor    proc    near
  289.     push    bx
  290.     push    cx
  291.     push    dx
  292.     push    di
  293. ;
  294. ; Point ES:DI to the display memory byte in which the pixel goes, with
  295. ; the bit mask set up to access that pixel within the addressed byte.
  296. ;
  297.     push    ax        ;preserve dot color
  298.     mov    ax,SCREEN_WIDTH_IN_BYTES    
  299.     mul    cx        ;offset of start of top scan line
  300.     mov    di,ax
  301.     mov    cl,bl
  302.     and    cl,111b
  303.     mov    dx,GC_INDEX
  304.     mov    al,BIT_MASK
  305.     out    dx,al
  306.     inc    dx
  307.     mov    al,80h
  308.     shr    al,cl
  309.     out    dx,al        ;set the bit mask for the pixel
  310.     shr    bx,1
  311.     shr    bx,1
  312.     shr    bx,1        ;X in bytes
  313.     add    di,bx        ;offset of byte pixel is in
  314.     mov    al,es:[di]    ;load latches
  315.     pop    ax        ;get back dot color
  316.     stosb            ;write dot in desired color
  317.  
  318.     pop    di
  319.     pop    dx
  320.     pop    cx
  321.     pop    bx
  322.     ret
  323. DotUpInColor    endp
  324. Start    endp
  325. Code    ends
  326.     end    Start
  327.  
  328.